//
//  BTDetailConsultVC.m
//  BaxTer
//
//  Created by 陈武琦 on 16/9/21.
//  Copyright © 2016年 陈武琦. All rights reserved.
//

#import "BTDetailConsultVC.h"

@interface BTDetailConsultVC ()

#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_8_0
@property (nonatomic, strong) WKWebView *webView;
#else
@property (nonatomic, strong) UIWebView *webView;
#endif

@property(nonatomic, copy)NSString *url;
@property (nonatomic, strong) UIProgressView *progressView;

@end

@implementation BTDetailConsultVC

- (instancetype)initWithUrl:(NSString *)url {

    if (self = [super init]) {
        _url = url;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.webView];
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
    [self appendProgressView];
    
    // Do any additional setup after loading the view.
}

- (void)dealloc {
    [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
    [self.webView removeFromSuperview];
    self.webView = nil;
}

#pragma mark - Getters
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
- (WKWebView *)webView {
    if (_webView) return _webView;
    _webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
    _webView.navigationDelegate = self;
    _webView.UIDelegate = self;
    return _webView;
}

#else
- (UIWebView *)webView {
    if (_webView) return _webView;
    _webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    _webView.delegate = self;
    _webView.scalesPageToFit = YES;
    _webView.translatesAutoresizingMaskIntoConstraints = NO;
    return _webView;
}
#endif

#pragma mark - WKNavigation delegate

- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    
    if (!self.navigationItem.title) {
        self.navigationItem.title = webView.title;
    }
}

- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}


#pragma mark - ProgressView
- (UIProgressView *)progressView {
    if (!_progressView) {
        _progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
    }
    return _progressView;
}

- (void)appendProgressView {
    [self.view addSubview:self.progressView];
    [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionInitial context:nil];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:@"estimatedProgress"]) {
        self.progressView.hidden = self.webView.estimatedProgress == 1;
        float progress = self.progressView.hidden ? 0 : self.webView.estimatedProgress;
        [self.progressView setProgress:progress animated:YES];
    }
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.progressView.frame = CGRectMake(0, self.topLayoutGuide.length, self.view.frame.size.width, 0.5);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
